package com.lam.mall.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.constant.MallStateConstant;
import com.lam.common.context.TokenUtils;
import com.lam.common.utils.StringUtils;
import com.lam.mall.entity.OrderAddr;
import com.lam.mall.entity.OrderLog;
import com.lam.mall.mapper.OrderAddrMapper;
import com.lam.mall.mapper.OrderLogMapper;
import com.lam.mall.param.OrderAddrParam;
import com.lam.mall.service.OrderAddrService;

/**
 * 订单配送地址Service业务层处理
 * 
 * @author admin
 * @date 2023-04-25
 */
@Service
public class OrderAddrServiceImpl  implements OrderAddrService {

    @Autowired
    private OrderAddrMapper orderAddrMapper;
    @Autowired
    private OrderLogMapper orderLogMapper;

    /**
     * 查询订单配送地址
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public OrderAddr selectById(String id) {

        return orderAddrMapper.selectById(id);
    }

    /**
     * 查询订单配送地址列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<OrderAddr> selectList(OrderAddrParam param) {
    
        QueryWrapper<OrderAddr> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.eq(StringUtils.isNotBlank(param.getUserId()), "user_id", param.getUserId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getOrderNumber()), "order_number", param.getOrderNumber());

        return orderAddrMapper.selectList(queryWrapper);
    }

    /**
     * 新增订单配送地址
     * 
     * @param orderAddr 
     * @return 
     */
    @Override
    @Transactional
    public int insert(OrderAddr orderAddr) {
    
        return orderAddrMapper.insert(orderAddr);
    }

    /**
     * 修改订单配送地址
     * 
     * @param orderAddr 
     * @return 
     */
    @Override
    @Transactional
    public int update(OrderAddr orderAddr) {
    
    	OrderLog orderLog = new OrderLog();
    	orderLog.setCreateBy(TokenUtils.getLoginUsername());
    	orderLog.setCreateTime(new Date());
    	orderLog.setOperType(MallStateConstant.ORDER_OPER_TYPE_SELLER);
    	orderLog.setOperRemark("修改订单地址！");
    	orderLog.setOrderNumber(orderAddr.getOrderNumber());
		orderLogMapper.insert(orderLog );
        return orderAddrMapper.updateById(orderAddr);
    }

    /**
     * 删除订单配送地址信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return orderAddrMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
    
    
}
